<template>
    <div>
        <!-- 搜索表单 -->
        <el-form :inline="true" :model="searchFrom" ref="searchFrom" class="demo-form-inline" style="text-align:center">
            <el-form-item label="编号" prop="id">
            <el-input v-model="searchFrom.id" placeholder="请输入编号"></el-input>
        </el-form-item>
            <el-form-item label="名字" prop="name">
                <el-input v-model="searchFrom.name" placeholder="请输入名字"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="searchBtn()">查询</el-button>
                <el-button type="warning" icon="el-icon-refresh" @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="id" label="编号"/>
            <el-table-column prop="name" label="维修厂名称"/>
            <el-table-column prop="address" label="地址"/>
            <el-table-column prop="phoneNum" label="电话"/>
            <el-table-column prop="addtime" label="添加时间"/>
            <el-table-column prop="remark" label="评论"/>
            <el-table-column align="right">
                <template slot="header">
                    <span style="margin-right:30px">操作</span>
                    <el-button size="mini" type="primary" @click="openAddDialog()">添加</el-button>
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="block" style="text-align:center">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[1, 2, 5, 10]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <!-- 添加/修改弹出框 -->
        <el-dialog title="添加/修改维修厂" :visible.sync="dialogAdd">
            <el-form :model="shop" ref="addShop" :rules="rules">
                <el-input v-model="shop.id" style="display:none;"/>
                <el-form-item label="维修厂名称" label-width="120px" prop="name">
                    <el-input v-model="shop.name" :min="1" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" label-width="120px" prop="address">
                    <el-input v-model="shop.address" :min="1" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" label-width="120px" prop="phoneNum">
                    <el-input v-model="shop.phoneNum" :min="1" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="添加时间" label-width="120px" prop="addtime">
                    <el-col :span="11">
                        <el-form-item prop="addtime">
                            <el-date-picker type="date"
                                            format="yyyy-MM-dd"
                                            value-format="yyyy-MM-dd"
                                            placeholder="选择日期" v-model="shop.addtime"
                                            style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-form-item>
                <el-form-item label="备注" label-width="120px" prop="remark">
                    <el-input v-model="shop.remark" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAdd=false">取 消</el-button>
                <el-button type="primary" @click="addShopBtn()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        // data() {
        data: function () {
            // 自定义验证规则 验证添加和修改时的品牌名字是否可用
            const checkName = (rule, value, callback) => {
                this.$http.get("/cs/maintainShop/checkName?name=" + value + "&id=" + this.shop.id)
                    .then((response) => {
                        if (response.data.resultCode === 5008) {
                            callback(new Error('该维修厂已存在！'));
                        } else {
                            callback();
                        }
                    })
            };
            return {
                tableData: [],
                total: 0, // 总条数
                pageNum: 1, // 当前页数
                pageSize: 5, // 每页显示几条
                dialogAdd: false,
                searchFrom: {},
                shop: {},  // 添加和编辑的时候的通用修理厂信息
                rules: {
                    name : [
                        {required: true, message: '请输入维修厂名称', trigger: 'blur'},
                        {trigger: 'blur', validator: checkName}//自定义验证:调方法
                    ],
                    address: [
                        {required: true, message: '请输维修厂地址', trigger: 'blur'}
                    ],
                    phoneNum: [
                        {required: true, message: '请输入维修厂电话', trigger: 'blur'},
                    ],
                    addtime:[
                        {required: true, message: '请输入时间', trigger: 'blur'},
                    ],
                    remark:[
                        {required: true, message: '请输入备注', trigger: 'blur'},
                    ],
                },
            }
        },
        methods: {
            // 重置表格
            resetForm() {
                this.searchFrom={};
                this.getAllShop();
            },
            // 添加按钮
            openAddDialog() {
                // 设置添加的初始值
                this.shop = {};
                this.dialogAdd = true;
            },
            // 打开编辑操作
            handleEdit(index, row) {
                this.dialogAdd = true // 打开弹出层
                // 让表单重新赋值
                this.shop = JSON.parse(JSON.stringify(row))
            },
            //每页显示几条改变
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAllShop();
            },
            // 当前是第几页改变
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAllShop();
            },
            // 查询表单提交
            searchBtn() {
                this.pageNum = 1;
                this.getAllShop();
            },
            // 获得表格数据
            getAllShop() {
                this.$http.post('cs/maintainShop/queryAll?pageSize=' + this.pageSize + '&pageNo=' + this.pageNum, this.searchFrom)
                    .then(res => {
                        this.tableData = res.data.t.list;
                        this.total  = res.data.t.total
                    })
            },
            // 添加表单提交
            addShopBtn() {
                this.$refs["addShop"].validate((valid) => {
                    if (valid){
                        if (this.shop.id === undefined){
                            this.$http.post("cs/maintainShop/insert", this.shop).then(res => {
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，添加成功'});
                                    this.dialogAdd = false;
                                    this.getAllShop();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }else {
                            this.$http.put("cs/maintainShop/update", this.shop).then(res => {
                                console.log(res.data.t);
                                if (Number(res.data.resultCode) === 2000) {
                                    this.$message({type: 'success', message: '恭喜你，修改成功'});
                                    this.dialogAdd = false;
                                    this.getAllShop();
                                } else {
                                    this.$message({type: 'error', message: '系统异常'});
                                }
                            })
                        }

                    }
                })
            },
            // 删除
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', center: true
                }).then(() => {
                    this.$http.delete("cs/maintainShop/deleteById?maintainShopId=" + row.id).then(res => {
                        if (Number(res.data.resultCode) === 2000) {
                            this.$message({type: 'success', message: '删除成功!'});
                            this.getAllShop();
                        } else {
                            this.$message({type: 'error', message: '系统异常'});
                        }
                    })
                }).catch(() => {
                    this.$message({type: 'info', message: '已取消删除'});
                });
            }
        },
        mounted() {
            this.getAllShop();
        }
    }
</script>

<style scoped>

</style>